package com.personal.website.lottery.spider;

import com.personal.website.lottery.util.PropertiesUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

/**
 * Created by 10171225 on 2016/2/17.
 */

public class SpiderProperties {
    public static final String HOST_URL = "hostUrl";
    public static final String RED_BALLS_REGEX = "redBallsRegex";
    public static final String BLUE_BALL_REGEX = "blueBallRegex";
    public static final String CURRENT_PRIZE_POOL_REGEX = "currentPrizePoolRegex";
    public static final String ISSUE_REGEX = "issueRegex";
    public static final String DRAW_DATE_REGEX = "drawDateRegex";
    public static final String PRIZE_RECORDS_REGEX = "prizeRecordsRegex";
    public static final String INTERVAL = "interval";
    public static final String DAILY_START_TIME = "dailyStartTime";
    public static final String JDBC_DRIVER = "jdbc.driver";
    public static final String JDBC_URL = "jdbc.url";
    public static final String JDBC_USERNAME = "jdbc.username";
    public static final String JDBC_PASSWORD = "jdbc.password";

    public static final String SPIDER_PROPERTIES_PATH = "spider.properties";

    private Properties prop = new Properties();

    public SpiderProperties() {
        prop = PropertiesUtil.getProperty(SPIDER_PROPERTIES_PATH);
    }

    public String getHostUrl() {
        return prop.getProperty(HOST_URL);
    }

    public List<String> getRedBallsRegex() {
        return getRegexs(RED_BALLS_REGEX);
    }

    public List<String> getPrizeRecordsRegex() {
        return getRegexs(PRIZE_RECORDS_REGEX);
    }

    public String getCurrentPrizePoolRegex() {
        return prop.getProperty(CURRENT_PRIZE_POOL_REGEX);
    }

    public String getBlueBallRegex() {
        return prop.getProperty(BLUE_BALL_REGEX);
    }

    public String getIssueRegex() {
        return prop.getProperty(ISSUE_REGEX);
    }

    public String getDrawDateRegex() {
        return prop.getProperty(DRAW_DATE_REGEX);
    }

    private List<String> getRegexs(String regex) {
        List<String> regexs = new ArrayList<String>();
        List<String> keys = new ArrayList<String>();
        for (Object key : prop.keySet()) {
            keys.add(key.toString());
        }
        Collections.sort(keys);
        for (String key : keys) {
            if (key.startsWith(regex)) {
                regexs.add(prop.getProperty(key));
            }
        }
        return regexs;
    }

    public String getInterval() {
        return prop.getProperty(INTERVAL);
    }

    public String getDailyStartTime() {
        return prop.getProperty(DAILY_START_TIME);
    }

    public String getJdbcDriver() {
        return prop.getProperty(JDBC_DRIVER);
    }

    public String getJdbcUrl() {
        return prop.getProperty(JDBC_URL);
    }

    public String getJdbcUsername() {
        return prop.getProperty(JDBC_USERNAME);
    }

    public String getJdbcPassword() {
        return prop.getProperty(JDBC_PASSWORD);
    }
}
